//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.crazy.plugins.gen;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import cn.org.rapid_framework.generator.GeneratorProperties;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
/**
 * @className AbstarctGeneratorMojo
 * @description //TODO 描述类的功能
 * @author zhouzhendong
 * @date 2019/7/30 17:18
 * @version V1.0
 * descriptor 1
 */
public abstract class AbstarctGeneratorMojo extends  AbstractMojo {
    @Parameter( defaultValue = "${project}", readonly = true )
    private MavenProject project = new MavenProject();

    public AbstarctGeneratorMojo() {
    }

    protected ClassLoader getClassLoader() {
        try {
            Collection<URL> urls = new ArrayList();
            Iterator i$ = this.project.getCompileClasspathElements().iterator();

            while(i$.hasNext()) {
                String elements = (String)i$.next();
                urls.add((new File(elements)).toURI().toURL());
            }

            i$ = this.project.getArtifacts().iterator();

            while(i$.hasNext()) {
                Artifact artifact = (Artifact)i$.next();
                urls.add(artifact.getFile().toURI().toURL());
            }

            System.out.println("urls = \n" + urls.toString().replace(",", "\n"));
            return new URLClassLoader((URL[])urls.toArray(new URL[urls.size()]), this.getClass().getClassLoader());
        } catch (Exception var4) {
            var4.printStackTrace();
            System.out.println("failed to get the classloader.");
            return this.getClass().getClassLoader();
        }
    }

    public MavenProject getProject() {
        return this.project;
    }

    protected String[] parseStringArray(String string) {
        return com.crazy.plugins.gen.AbstarctGeneratorMojo.MojoUtil.parseStringArray(string);
    }

    protected boolean isOsWindows() {
        return com.crazy.plugins.gen.AbstarctGeneratorMojo.MojoUtil.isOsWindows();
    }
    public static class MojoUtil {
        public static String[] parseStringArray(String string) {
            return string.split(",");
        }

        public static boolean isOsWindows() {
            return System.getProperty("os.name").toUpperCase().contains("Windows".toUpperCase());
        }

        public static String toConsistentFilePath(String path) {
            return new File(path).getAbsolutePath();
        }

        public static void openFileIfOnWindows() throws IOException {
            if (isOsWindows())
                Runtime.getRuntime().exec("cmd.exe /c start " + toConsistentFilePath(GeneratorProperties.getRequiredProperty("outRoot")));
        }
    }
}
